package com.cwd.arm.infrastructure.dao.base;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwd.arm.common.query.PageQuery;
import com.cwd.arm.common.util.SessionUtil;
import com.cwd.arm.infrastructure.mapper.UserMapper;
import com.cwd.arm.infrastructure.po.UserPo;
import com.cwd.arm.infrastructure.po.base.BasePo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by yan on 21-12-7.
 */
public abstract class DaoImpl<M extends BaseMapper<T>, T extends BasePo> extends ServiceImpl<M, T> {

    @Autowired
    protected UserMapper userMapper;

    @Override
    public boolean save(T entity) {
        setSave(entity);
        return super.save(entity);
    }

    @Override
    public boolean saveBatch(Collection<T> entityList) {
        setSave(entityList);
        return super.saveBatch(entityList);
    }

    @Override
    public boolean updateById(T entity) {
        setUpdate(entity);
        return super.updateById(entity);
    }


    @Override
    public T getById(Serializable id) {
        T t = super.getById(id);
        if (t != null && t.getUserId() != null) {
            UserPo userPo = userMapper.selectById(t.getUserId());
            if (userPo != null) {
                setUserInfo(t, userPo);
            }
        }
        return t;
    }

    @Override
    public List<T> list(Wrapper<T> queryWrapper) {
        List<T> list = super.list(queryWrapper);
        setUserInfo(list);
        return list;
    }

    protected List<T> baseList(Wrapper<T> queryWrapper) {
        return super.list(queryWrapper);
    }

    protected void setUserInfo(List<T> list) {
        List<UserPo> userPoList = getUserList();
        for (T t : list) {
            UserPo userPo = getUserPo(t.getUserId(), userPoList);
            if (userPo != null) {
                setUserInfo(t, userPo);
            }
        }
    }

    protected List<UserPo> getUserList(){
        QueryWrapper<UserPo> userQueryWrapper = new QueryWrapper<>();
        return userMapper.selectList(userQueryWrapper);
    }


    protected void setUserInfo(T t, UserPo userPo) {
        if(StringUtils.isNotEmpty(userPo.getRealName())){
            t.setShowName(userPo.getRealName());
        }else{
            t.setShowName(userPo.getUsername());
        }
    }

    protected UserPo getUserPo(Long userId, List<UserPo> userPoList) {
        if (userId == null) {
            return null;
        }
        return userPoList.stream().filter(po -> po.getId().compareTo(userId) == 0).findAny().orElse(null);
    }

    protected void setSort(PageQuery daoQuery, QueryWrapper<T> queryWrapper) {
        if(daoQuery.getOrderByType().equals("desc") || daoQuery.getOrderByType().equals("DESC")){
            queryWrapper.orderByDesc(ObjectUtils.isNotEmpty(daoQuery.getSortField()),daoQuery.getSortColumn());
        }else{
            queryWrapper.orderByAsc(ObjectUtils.isNotEmpty(daoQuery.getSortField()),daoQuery.getSortColumn());

        }
    }

    private void setSave(T entity) {
        entity.setUserId(SessionUtil.getUserId());
        Date date = new Date();
        entity.setCreateTime(date);
        entity.setUpdateTime(date);
    }

    private void setSave(Collection<T>  entityList) {
        for(T entity : entityList){
            entity.setUserId(SessionUtil.getUserId());
            Date date = new Date();
            entity.setCreateTime(date);
            entity.setUpdateTime(date);
        }
    }

    private void setUpdate(T entity) {
        entity.setUserId(SessionUtil.getUserId());
        Date date = new Date();
        entity.setUpdateTime(date);
    }

}
